package cn.git.agent;

import cn.git.agent.enhance.ClassEnhancePluginDefine;
import cn.git.agent.interceptor.ConstructMethodInterceptPoint;
import cn.git.agent.interceptor.InstanceMethodInterceptPoint;
import cn.git.agent.interceptor.StaticMethodInterceptPoint;
import cn.git.agent.match.ClassMatch;
import cn.git.agent.match.MultiClassNameMatch;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;

/**
 * @description: 定义mysql5的拦截插件
 * @program: bank-credit-sy
 * @author: lixuchun
 * @create: 2024-12-24
 */
public class Mysql5Instrumentation extends ClassEnhancePluginDefine {

    /**
     * 拦截的方法
     */
    private static final String INSPECT_EXECUTE_METHOD = "execute";
    private static final String INSPECT_EXECUTE_QUERY = "executeQuery";
    private static final String INSPECT_EXECUTE_UPDATE = "executeUpdate";
    private static final String INSPECT_EXECUTE_LARGE_UPDATE = "executeLargeUpdate";

    /**
     * 拦截器类全路径名称
     */
    private static final String INTERCEPTOR_CLASS_NAME = "cn.git.agent.interceptor.Mysql5Interceptor";

    /**
     * 拦截类全路径名称
     */
    public static final String PREPARED_STATEMENT_NAME = "com.mysql.jdbc.PreparedStatement";

    /**
     * 获取需要拦截的类
     *
     * @return
     */
    @Override
    protected ClassMatch enhanceClass() {
        return MultiClassNameMatch.byMultiClassMatch(PREPARED_STATEMENT_NAME);
    }

    /**
     * 获取需要拦截的方法，实例拦截方法
     *
     * @return
     */
    @Override
    protected InstanceMethodInterceptPoint[] getInstanceMethodsInterceptPoints() {
        return new InstanceMethodInterceptPoint[] {
                new InstanceMethodInterceptPoint() {
                    /**
                     * 获取需要拦截的方法，作为method()方法的参数
                     *
                     * @return
                     */
                    @Override
                    public ElementMatcher<MethodDescription> getMethodsMatcher() {
                        return ElementMatchers.namedOneOf(INSPECT_EXECUTE_METHOD,
                                INSPECT_EXECUTE_QUERY,
                                INSPECT_EXECUTE_UPDATE,
                                INSPECT_EXECUTE_LARGE_UPDATE);
                    }

                    /**
                     * 获取需要拦截的方法的拦截器
                     *
                     * @return
                     */
                    @Override
                    public String getMethodInterceptor() {
                        return INTERCEPTOR_CLASS_NAME;
                    }
                }
        };
    }

    /**
     * 获取构造方法的拦截点
     *
     * @return
     */
    @Override
    protected ConstructMethodInterceptPoint[] getConstructMethodInterceptPoint() {
        return new ConstructMethodInterceptPoint[0];
    }

    /**
     * 获取静态方法拦截点
     *
     * @return
     */
    @Override
    protected StaticMethodInterceptPoint[] getStaticMethodInterceptPoint() {
        return new StaticMethodInterceptPoint[0];
    }
}
